package com.beetle.bauhinia.glide;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.app.Fragment;

import com.beetle.imkit.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;

/**
 * Created by Gorden on 2017/9/25.
 * glide 封装
 */

public class GlideUtils {


    private static CenterCrop centerCrop;
    private static CircleCrop circleCrop;
    private static Transformation[] avatarTransformations;

    public static RequestManager with(Fragment fragment) {
        RequestManager requestManager = Glide.with(fragment);
        initRequestManager(requestManager);
        return requestManager;
    }

    public static RequestManager with(Context context) {
        RequestManager requestManager = Glide.with(context);
        initRequestManager(requestManager);
        return requestManager;
    }

    public static RequestManager with(Context context,int pl) {
        RequestManager requestManager = Glide.with(context);
        initRequestManager(requestManager,pl);
        return requestManager;
    }

    public static RequestManager with(Activity activity) {
        RequestManager requestManager = Glide.with(activity);
        initRequestManager(requestManager);
        return requestManager;
    }

    public static RequestManager with(Context context,boolean isAvatar) {
        RequestManager requestManager = Glide.with(context);
        if (isAvatar){
            initRequestManager(requestManager);
        }else{
            requestManager.setDefaultRequestOptions(configDefaultRequestOptions());
        }
        return requestManager;
    }

    public static RequestManager with(Activity activity,boolean isAvatar) {
        RequestManager requestManager = Glide.with(activity);
        if (isAvatar){
            initRequestManager(requestManager);
        }else{
            requestManager.setDefaultRequestOptions(configDefaultRequestOptions());
        }
        return requestManager;
    }

    @SuppressLint("CheckResult")
    private static void initRequestManager(RequestManager requestManager) {
        final RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.avatar_contact);
        options.error(R.drawable.avatar_contact);
        options.diskCacheStrategy(DiskCacheStrategy.NONE);
        options.skipMemoryCache(false);
        options.format(DecodeFormat.PREFER_RGB_565);
        options.error(R.drawable.avatar_contact);
        options.fallback(R.drawable.avatar_contact);
        requestManager.setDefaultRequestOptions(options);
    }

    @SuppressLint("CheckResult")
    private static void initRequestManager(RequestManager requestManager, int pl) {
        final RequestOptions options = new RequestOptions();
        options.placeholder(pl);
        options.error(pl);
        options.diskCacheStrategy(DiskCacheStrategy.NONE);
        options.format(DecodeFormat.PREFER_RGB_565);
        options.error(pl);
        options.fallback(pl);
        requestManager.setDefaultRequestOptions(options);
    }

    /**
     * 圆形图像transformations
     * @return
     */
    public static Transformation<Bitmap>[] getAvatarTransformations() {
        if (avatarTransformations == null) {
            avatarTransformations = new Transformation[]{
                    getCenterTransformations(),getCircleTransformations()
            };
        }
        return avatarTransformations;
    }

    public static CenterCrop getCenterTransformations() {
        if (centerCrop == null) {
            centerCrop = new CenterCrop();
        }
        return centerCrop;
    }


    public static CircleCrop getCircleTransformations() {
        if (circleCrop == null) {
            circleCrop = new CircleCrop();
        }
        return circleCrop;
    }

    public static RequestOptions courseCoverOptions(){
        final RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.image_download_fail);
        options.error(R.drawable.image_download_fail);
        options.error(R.drawable.image_download_fail);
        options.fallback(R.drawable.image_download_fail);
        return options;
    }

    public static RequestOptions configDefaultRequestOptions() {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.image_download_fail);
        requestOptions.error(R.drawable.image_download_fail);
        requestOptions.format(DecodeFormat.PREFER_RGB_565);
        return requestOptions;
    }
}
